import service from './service';
/*
 * JSON数组去重
 * @param: [array] json Array
 * @param: [string] 唯一的key名，根据此键名进行去重
 */
const uniqueArray = (array : any, key : string) => {
	let result = [array[0]];
	for (let i = 0; i < array.length; i++) {
		let item = array[i];
		let repeat = false;
		for (let j = 0; j < result.length; j++) {
			if (item[key] == result[j][key]) {
				repeat = true;
				break;
			}
		}
		if (!repeat) {
			result.push(item);
		}
	}
	return result;
}

/*
 * JSON删除特定
 * @param: [array] json Array
 * @param: {Object} json Object
 * @param: [string] 唯一的key名，根据此键名进行去重
 */

interface IItem {
	[key : string] : any;
}
const delArray = (array : IItem[], object : IItem, key : string) => {
	let result = [];
	for (let i = 0; i < array.length; i++) {
		let item = array[i];
		if (item[key] != object[key]) {
			result.push(item);
		}
	}
	return result;
}
interface backItem {
	[propName : string] : string | number
}
interface dialogParms {
	WhereList ?: backItem[],
	CallBack ?: (item : backItem) => void,//回调函数
	CallBackValid ?: (item : backItem) => void,//对选择荐的数据验证事件
	IsMult ?: string,//是否支持多选
	SelectionType : string,//调用组件的名称
	SelectionAlias ?: string,//调用组件的别名，用于不同的用途调用同一个组件时区分
	SelectedItems ?: string,//已选择的值，传key(一般是编码)，多个以英文逗号分隔
	SelectionStoreId ?: string,//门店
	IsNotPage ?: boolean,//是否开启分页
	IsStatust ?: string,//是否固定传状态字段，如果有则隐藏状态条件，改为传入的固定值
}
let setDialogParms : {};
enum navigateUrl {
	//#ifdef MP-WEIXIN
	"机构选择" = "/packages/ComDlgSelections/dlgSelStoreMth",
	"部门选择" = "/packages/ComDlgSelections/dlgSelDepartMth",
	"人员选择" = "/packages/ComDlgSelections/dlgSelUserMth",
	"仓库选择" = "/packages/ComDlgSelections/dlgSelWarehouse",
	//#endif

	//#ifndef MP-WEIXIN
	"机构选择" = "/packages/ComDlgSelections/dlgSelStoreMth",
	"部门选择" = "/packages/ComDlgSelections/dlgSelDepartMth",
	"人员选择" = "/packages/ComDlgSelections/dlgSelUserMth",
	"仓库选择" = "/pages/ComDlgSelections/dlgSelWarehouse",
	//#endif
}
const GetDialogParms = () : dialogParms => {
	let parms = {
		IsMult: "否",
		SelectionType: "",
		SelectedItems: "",
		SelectionAlias: "",
		SelectionStoreId: "",
		WhereList: [],
		IsNotPage: false,
		IsStatust: "",
	}
	return parms;
}
const OpenDialog = (parms : dialogParms) => {
	if (!parms.IsMult) {
		parms.IsMult = '否'
	}
	setDialogParms = parms;
	if (!parms.SelectionType) {
		uni.showToast({
			duration: 3500,
			icon: 'none',
			title: '弹框参数错误!',
		});
		return;
	}
	let dialogUrl = "";
	dialogUrl = navigateUrl[parms.SelectionType];
	dialogUrl += ("?IsMult=" + parms.IsMult);
	if (parms.SelectedItems) {
		dialogUrl += ("&SelectedItems=" + parms.SelectedItems)
	}
	if (parms.WhereList) {
		dialogUrl += ("&WhereList=" + JSON.stringify(parms.WhereList))
	}
	if (parms.SelectionStoreId) {
		dialogUrl += ("&SelectionStoreId=" + parms.SelectionStoreId)
	}
	if (parms.IsNotPage) {
		dialogUrl += ("&IsNotPage=" + parms.IsNotPage)
	}
	if (parms.IsStatust) {
		dialogUrl += ("&IsStatust=" + parms.IsStatust)
	}
	console.log('[dialogUrl',dialogUrl);
	
	uni.navigateTo({
		url: dialogUrl,
	});
}

const DialogCallBack = (item : backItem) => {
	if (setDialogParms && setDialogParms.CallBack) {
		setDialogParms.BackParms = item;
		setDialogParms.CallBack(setDialogParms)
	}
	uni.navigateBack({
		delta: 1
	});
}
const DialogCallBackValid = (item : backItem) => {
	if (setDialogParms && setDialogParms.CallBack) {
		setDialogParms.BackParms = item;
		setDialogParms.CallBackValid(setDialogParms)
	}
}
const GeolocationPosition = (MethodName : string, CallBack : () => void, errorCallback : (message : string) => void) => {
	if (CallBack) {
		// #ifdef APP-PLUS
		plus.geolocation.getCurrentPosition(CallBack, function (e) {
			if (errorCallback) errorCallback(e.message);
		});
		// #endif
	}
	// var AmapArray = plus.storage.getItem("MobileAmap")?JSON.parse(plus.storage.getItem("MobileAmap")):[];
	// var num = 0;
	// for(var cp=0;cp<AmapArray.length;cp++){
	// 	if(AmapArray[cp].c_api_name==MethodName){
	// 		num+=1;
	// 		AmapArray[cp].c_times+=1;
	// 		AmapArray[cp].c_dt=Enjoy.formatDate(new Date(),'yyyy-MM-dd HH:mm:ss')
	// 	}
	// }
	// if(num==0){
	// 	AmapArray.push({
	// 		c_cloud_id:JSON.parse(plus.storage.getItem("CUSTOMEROBJECT")).ID,//客户代码
	// 		c_api_name:MethodName,//接口名称
	// 		c_dt:Enjoy.formatDate(new Date(),'yyyy-MM-dd HH:mm:ss'),//日期
	// 		c_times:1,//次数
	// 	})
	// }
	// plus.storage.setItem("MobileAmap",JSON.stringify(AmapArray));
}
export default {
	uniqueArray,
	delArray,
	OpenDialog,
	GetDialogParms,
	DialogCallBack,
	DialogCallBackValid,
	GeolocationPosition
}